package com.weigee.ktv.v.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.l.d.app.library.eventbus.EventSend;
import com.l.d.app.library.eventbus.EventSendEntity;
import com.weigee.ktv.R;
import com.weigee.ktv.R2;
import com.weigee.ktv.m.EventTag;
import com.weigee.ktv.m.observer.DrawerLayoutObserver;
import com.weigee.ktv.m.subscribe.DrawerLayoutSubscribe;

import org.simple.eventbus.Subscriber;

import butterknife.BindView;
import io.reactivex.Observable;

/**
 * Created by 林德 on 2017/4/12 15:27.
 * <p/>
 */
public class MainFragment extends BaseFragment implements DrawerLayout.DrawerListener
{
    @BindView(R2.id.drawer_layout)
    DrawerLayout mDrawerLayout;

    private DrawerLayoutSubscribe mDrawerLayoutSubscribe;
    private Observable<Integer> mDrawerLayoutObservable;
    private DrawerLayoutObserver mDrawerLayoutObserver;

    private FirstContentFragment mContentFragment;
    private UserProfileFragment mLeftMenuFragment;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
    {
        return inflater.inflate(R.layout.main_frag, container, false);
    }

    @Override
    protected void initView(View view, @Nullable Bundle savedInstanceState)
    {
        mDrawerLayout.addDrawerListener(this);

        FragmentTransaction ft = getChildFragmentManager().beginTransaction();
        if (mContentFragment != null) ft.remove(mContentFragment);
        if (mLeftMenuFragment != null) ft.remove(mLeftMenuFragment);
        mContentFragment = new FirstContentFragment();
        mLeftMenuFragment = new UserProfileFragment();
        ft.add(R.id.content_frame, mContentFragment).add(R.id.left_drawer, mLeftMenuFragment);
        ft.commit();

        mDrawerLayoutSubscribe = new DrawerLayoutSubscribe();
        mDrawerLayoutObservable = Observable.create(mDrawerLayoutSubscribe);
        mDrawerLayoutObserver = new DrawerLayoutObserver(mDrawerLayout);
    }

    @Subscriber(tag = EventTag.EVENT_MAIN_DRAWER_LAYOUT_CHANGE_STATE)
    private void onDrawerLayoutChangeState(EventSendEntity entity)
    {
        Integer direction = entity.msg();
        if (direction == null) return;
        if (mDrawerLayoutSubscribe != null && mDrawerLayoutObservable != null && mDrawerLayoutObserver != null)
        {
            mDrawerLayoutSubscribe.setDirection(direction);
            mDrawerLayoutObservable.subscribe(mDrawerLayoutObserver);
        }
    }

    @Override
    public void onDrawerSlide(View drawerView, float slideOffset)
    {
    }

    @Override
    public void onDrawerOpened(View drawerView)
    {
    }

    @Override
    public void onDrawerClosed(View drawerView)
    {
    }

    @Override
    public void onDrawerStateChanged(int newState)
    {
        if (mDrawerLayout == null) return;
        if (!mDrawerLayout.isDrawerOpen(Gravity.START) && !mDrawerLayout.isDrawerOpen(Gravity.END))
        {
            EventSend.with(EventTag.EVENT_MAIN_DRAWER_ALL_CLOSE).msg("allClose").build();
        } else
        {
            EventSend.with(EventTag.EVENT_MAIN_DRAWER_NOT_ALL_CLOSE).msg("notAllClose").build();
        }
    }
}
